﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WEEK10_上课演示
{
    public partial class WebForm1 : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ShowData();


                for (int i = 0; i < GridView1.Rows.Count; i++)
                {
                    GridView1.Rows[i].Attributes.Add("onmouseover",
                                                     "color = this.style.backgroundColor;this.style.backgroundColor='#EAFCD5'");
                    GridView1.Rows[i].Attributes.Add("onmouseout", "this.style.backgroundColor=color");
                }
            }
        }

        private void ShowData()
        {
            string connectionString = "data source=10.22.149.99;database=School;uid=Student;pwd=123456";
            var connection = new SqlConnection(connectionString);
            string commandText = "SELECT * FROM Student";

            var dataAdapter = new SqlDataAdapter(commandText, connection);


            var dataSet = new DataSet();
            dataAdapter.Fill(dataSet, "Student");


            GridView1.DataSource = dataSet;
            GridView1.DataBind();
        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                GridView1.Rows[i].Cells[0].Text = (i + 1).ToString();
            }

            //foreach (GridViewRow row in GridView1.Rows)
            //{
            //    row.Cells[0].Text = "a";
            //}
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = GridView1.SelectedIndex;

            string studentID = GridView1.Rows[index].Cells[1].Text;

            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                GridView1.Rows[i].BackColor = Color.Transparent;
                GridView1.Rows[i].Attributes.Add("onmouseover",
                                                 "color = this.style.backgroundColor;this.style.backgroundColor='#EAFCD5'");
            }

            //foreach(GridViewRow row in GridView1.Rows)
            //{
            //    row.BackColor = Color.Transparent;
            //}


            GridView1.Rows[index].BackColor = Color.LightPink;
        }


        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string studentID = GridView1.Rows[e.RowIndex].Cells[1].Text;

            String commandText = string.Format("DELETE FROM Student WHERE StudentID='{0}'", studentID);
            SqlConnection connection = SqlHelper.GetConnection();
            SqlHelper.ExecuteNonQuerySqlCommand(commandText, connection);
            ShowData();
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            //Label1.Text= GridView1.Rows[e.NewEditIndex].Cells[1].Text;
            Session["oldStudentID"] = GridView1.Rows[e.NewEditIndex].Cells[1].Text;
            GridView1.EditIndex = e.NewEditIndex;
            ShowData();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            ShowData();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            //string oldStudentID = Label1.Text;
            string oldStudentID = Session["oldStudentID"].ToString();
            string studentID = (GridView1.Rows[e.RowIndex].Cells[1].Controls[0] as TextBox).Text;
            string name = (GridView1.Rows[e.RowIndex].Cells[2].Controls[0] as TextBox).Text;

            string commandText =
                string.Format("UPDATE Student SET studentID='{0}', Name='{1}' WHERE studentID='{2}'", studentID, name,
                              oldStudentID);
            SqlConnection connection = SqlHelper.GetConnection();
            SqlHelper.ExecuteNonQuerySqlCommand(commandText, connection);
            GridView1.EditIndex = -1;
            ShowData();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            var selectedStudentID = new List<string>();

            foreach (GridViewRow row in GridView1.Rows)
            {
                var t = row.FindControl("CheckBox1") as CheckBox;
                if (t.Checked)
                {
                    selectedStudentID.Add(row.Cells[2].Text);
                }
            }

            foreach (string s in selectedStudentID)
            {
                Response.Write(s + "<br/>");
            }
        }

        protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
        {
            var selectAll = GridView1.HeaderRow.FindControl("CheckBox2") as CheckBox;
            foreach (GridViewRow row in GridView1.Rows)
            {
                var t = row.FindControl("CheckBox1") as CheckBox;
                t.Checked = selectAll.Checked;
            }
        }
    }
}